﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace The_LarX
{
    public class WebPageMethod
    {
        /// <summary>
        /// It's the location of the first {
        /// </summary>
        public int StartLine;
        /// <summary>
        /// It's the location of the final }
        /// </summary>
        public int EndLine;
        /// <summary>
        /// The name of the method
        /// </summary>
        public string Name;

        /// <summary>
        /// All the methods that have benn called inside this method
        /// </summary>
        public List<WebPageMethodCalls> MethodCalls;

        private LarWebPage Page;
        public WebPageMethod(int start, int end, string name, LarWebPage page)
        {
            this.StartLine = start;
            this.EndLine = end;
            this.Name = name;
            this.Page = page;
            MethodCalls = new List<WebPageMethodCalls>();
        }

        public void GetMethodCalls(List<string> lines)
        {
            for (int i = StartLine + 1; i < EndLine; i++)
            {
                if (lines[i].Contains("(") && lines[i].Contains(")"))
                {
                    // That should be a method
                    string args = lines[i].Remove(0, lines[i].IndexOf("("));
                    string metName = lines[i].Replace(args, "");
                    string variableName = "";
                    if (metName.Contains("$") && metName.Contains("="))
                    {
                        // It's initializing a var
                        int metSize = metName.IndexOf("=");
                        if (metName.Contains(" ="))
                            metSize--;
                        for (int l = 0; l < metSize; l++)
                        {
                            try
                            {
                                variableName += metName[l];
                            }
                            catch { }
                        }
                        Page.Variables.Add(variableName);
                        if (metName.Contains("= "))
                            metName = metName.Remove(0, metName.IndexOf("=") + 2);
                        else
                            metName = metName.Remove(0, metName.IndexOf("=") + 1);
                    }
                    WebPageMethodCalls call = new WebPageMethodCalls(i, args, metName);
                    call.AssociatedVariable = variableName;
                    this.MethodCalls.Add(call);
                }
            }
        }
    }
}
